using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Web.UI.WebControls;

	/// <summary>
	/// Summary description for DocTree.
	/// </summary>
    public partial class DocTree : System.Web.UI.Page
	{
		//protected Microsoft.Web.UI.WebControls.TreeView TreeCtrl1;
			
		public DocTree()
		{
			Page.Init += new System.EventHandler(Page_Init);
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			if ( ! this.IsPostBack )
			{
				// add tree node "type" for folders and files
				string imgurl = "/shark/webctrl_client/1_0/Images/";
				TreeNodeType type;

				type = new TreeNodeType();
				type.Type = "folder";
				type.ImageUrl = imgurl + "folder.gif";
				type.ExpandedImageUrl = imgurl + "folderopen.gif";
				TreeCtrl.TreeNodeTypes.Add( type );
	
				type = new TreeNodeType();
				type.Type = "file";
				type.ImageUrl = imgurl + "html.gif";
				TreeCtrl.TreeNodeTypes.Add( type );
			
				// start the recursively load from our application root path
				//  (we add the trailing "/" for a little substring trimming below)
				GetFolders( MapPath( "~/./" ), TreeCtrl.Nodes );

				// expand 3 levels of the tree
				TreeCtrl.ExpandLevel = 3;
			}
		}

		private void Page_Init(object sender, EventArgs e)
		{
			InitializeComponent();

		}

		// recursive method to load all folders and files into tree
        private void GetFolders(string path, Microsoft.Web.UI.WebControls.TreeNodeCollection nodes)
		{
			// add nodes for all directories (folders)
			string[] dirs = Directory.GetDirectories( path );
			foreach( string p in dirs )
			{
				string dp = p.Substring( path.Length );
				if ( dp.StartsWith( "_v" ) )
					continue; // ignore frontpage (Vermeer Technology) folders
				nodes.Add( Node( "", p.Substring( path.Length ), "folder" ) );
			}			

			// add nodes for all files in this directory (folder)
			string[] files = Directory.GetFiles( path, "*.aspx" );
			foreach( string p in files )
			{
				nodes.Add( Node( p, p.Substring( path.Length ), "file" ) );
			}			

			// add all subdirectories for each directory (recursive)
			for( int i = 0; i < nodes.Count; i++ )
			{
				if ( nodes[ i ].Type == "folder" )
				{
					GetFolders( dirs[ i ] + "\\", nodes[i ].Nodes );
				}
			}				
		}

		// create a TreeNode from the specified path, text and type
        private Microsoft.Web.UI.WebControls.TreeNode Node(string path, string text, string type)
		{
            Microsoft.Web.UI.WebControls.TreeNode n = new Microsoft.Web.UI.WebControls.TreeNode();
			n.Type = type;
			n.Text = text;
			if ( type == "file" )
			{
				// strip off the physical application root portion of path
				string nav = "~/" + path.Substring( MapPath( "~/" ).Length );
				nav.Replace( '\\', '/' );
				n.NavigateUrl =  nav;
				// set target if using FRAME/IFRAME
				n.Target="doc";
			}
			return n;
		}

		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}

